﻿using HtmlAgilityPack;
using Nlnet.Avalonia.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace 畜牧业审核程序国产.Models.ToolsClass
{
    internal class MySpider
    {
        static async Task<string> GetHtmlContentAsync(string url)
        {
            using (HttpClient client = new HttpClient())
            {
                // 设置默认请求头
                client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");
                client.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
                client.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.9");
                client.DefaultRequestHeaders.Add("Connection", "keep-alive");
                client.DefaultRequestHeaders.Add("Upgrade-Insecure-Requests", "1");
                try
                {
                    HttpResponseMessage response = await client.GetAsync(url);
                    response.EnsureSuccessStatusCode();
                    return await response.Content.ReadAsStringAsync();
                }
                catch (HttpRequestException e)
                {
                    MessageBox.Show($"Request exception: {e.Message}");
                    return null;
                }
            }
        }

        static async Task<HtmlDocument> GetHtmlDocAsync()
        {
            string url = "https://zhujia.zhuwang.com.cn/indexov.shtml";
            HtmlWeb htmlWeb = new HtmlWeb();
            htmlWeb.OverrideEncoding = Encoding.UTF8;


            string htmlContent = await GetHtmlContentAsync(url);
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(htmlContent);
            return doc;

        }
        public static async Task<Dictionary<string, string>> GetPriceAsync()
        {
            HtmlDocument doc = await GetHtmlDocAsync();
            //HtmlNode liNode = doc.DocumentNode.SelectSingleNode($"//li[@title='{类别}']");
            Dictionary<string, string> priceInfo_Dic = new Dictionary<string, string>();
            //if (liNode != null)
            //{
            //    // 从 li 元素中选择 b 标签
            //    HtmlNode bNode = liNode.SelectSingleNode(".//b[@class='red']");

            //    if (bNode != null)
            //    {
            //        // 提取 b 标签中的文本
            //        string value = bNode.InnerText;
            //        return value;
            //    }
            //}

            var liElements = doc.DocumentNode.SelectNodes("//li[@title='外三元' or @title='内三元' or @title='土杂猪' " +
                " or @title='玉米' or @title='豆粕'   or @title='猪粮比' or @title='仔猪集市价格' or @title='全国二元母猪销售价格'  ]");
            if (liElements != null)
            {
                foreach (var li in liElements)
                {
                    // 获取 <li> 元素的 title 属性
                    string title = li.GetAttributeValue("title", "");

                    // 获取价格信息
                    HtmlNode priceNode = null;
                    if (title.Equals("仔猪集市价格")|| title.Equals("全国二元母猪销售价格"))
                    {
                        priceNode = li.SelectSingleNode(".//span");
                    }
                    else
                    {
                        priceNode = li.SelectSingleNode(".//b");
                    }
                     
                    string price = priceNode?.InnerText ?? "价格未找到";

                    // 获取涨跌信息
                    var changeNode = li.SelectSingleNode(".//i/span");
                    string change = changeNode?.InnerText ?? "涨跌未找到";

                    priceInfo_Dic.Add(title, price);
                }
                return priceInfo_Dic;
            }


            return null;
        }
    }
}
